package org.lundberg.podcasts.util;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import org.lundberg.podcasts.R;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SubscriptionsAdapter extends CursorAdapter
{
	public static final String[] COLUMNS = new String[] {"_id",
														"title",
														"author",
														"imageURL",
														"new_items"};
	private HashMap<Integer, Bitmap> iconCache;
	
	public SubscriptionsAdapter (Context context, Cursor c, HashMap<Integer, Bitmap> iconCache) {
		super(context, c, false);
		this.iconCache = iconCache;
	}
	
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent)
	{	
		final LayoutInflater inflater = LayoutInflater.from(context);
		View view =  inflater.inflate(R.layout.subscriptions_row, parent, false);		
		return view;
	}
	
	
	@Override
	public void bindView(View view, final Context context, Cursor c)
	{
		final ImageView icon = (ImageView)view.findViewById(R.id.icon);
		
		if (iconCache.containsKey(c.getInt(0))) {
		
			icon.setImageBitmap(iconCache.get(c.getInt(0)));
		}
		else {
			icon.setImageResource(R.drawable.default_thumbnail);
			
			final int channel_id = c.getInt(0);
			final String url = c.getString(3);
			
			ViewTreeObserver vto = icon.getViewTreeObserver();
			vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
			    public boolean onPreDraw() {
			    	new SubscriptionsAdapterDownloadIconTask(context, channel_id, url, icon).execute();
			        icon.getViewTreeObserver().removeOnPreDrawListener(this);
			    	return true;
			    }
			});
			
		}
		
		
        int newItems = c.getInt(4);
        
        ImageView overlay = (ImageView)view.findViewById(R.id.icon_overlay);
        TextView count = (TextView)view.findViewById(R.id.count);
        count.setText(String.valueOf(newItems));
        
        if (newItems > 0)
        {
        	overlay.setVisibility(View.VISIBLE);
        	count.setVisibility(View.VISIBLE);
        }
        else {
        	
        	overlay.setVisibility(View.GONE);
        	count.setVisibility(View.GONE);
        }
        
        
        TextView title = (TextView)view.findViewById(R.id.title);
        title.setText(c.getString(1));
        
        TextView description = (TextView)view.findViewById(R.id.description);
        description.setText(c.getString(2));
	}
	
	
	
	
	private class SubscriptionsAdapterDownloadIconTask extends DownloadIconTask
    {
    	private WeakReference<Context> activity;
    	private int channel_id;
    	
    	public SubscriptionsAdapterDownloadIconTask(Context activity, int channel_id, String imageURL, ImageView view) {
    		
    		super(imageURL, view);
    		this.channel_id = channel_id;
    		this.activity = new WeakReference<Context>(activity);
    	}
    	
    	@Override
    	public void onPreExecute() {
    		
    		networkAvailable = Util.isNetworkAvailable(activity.get());
    	}
    	
    	@Override
    	protected void onPostExecute(Bitmap bitmap)
    	{    		
    		if (bitmap != null) {
				
				iconCache.put(channel_id, bitmap);
				
				ImageView view = imageViewReference.get();
				
				if (view != null)
				{
					TransitionDrawable drawable = new TransitionDrawable(
							new Drawable[] {view.getDrawable(), new BitmapDrawable(activity.get().getResources(), bitmap)});
					
					view.setImageDrawable(drawable);
					drawable.setCrossFadeEnabled(true);
					drawable.startTransition(500);
				}
    			
			}
			else {
				
				Log.e("SubscriptionsAdapterDownloadIconTask", "unable to create bitmap");
			}
    		
    	}
    }
		
}
